package dk.mirasola.systemtraining.server.entity;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.annotation.Unindexed;
import dk.mirasola.systemtraining.bridgewidgets.shared.factory.BiddingSequenceFactory;
import dk.mirasola.systemtraining.bridgewidgets.shared.factory.DistributionFactory;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.BiddingSequence;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Distribution;

import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import java.util.List;

public class Board {
    @Id
    long boardNumber;

    @Parent
    Key<BiddingSet> biddingSet;

    @Unindexed
    List<String> comments;

    @Unindexed
    String distributionStr;

    @Unindexed
    String biddingSequenceStr;

    @Transient
    Distribution distribution;

    @Transient
    BiddingSequence biddingSequence;

    public Board() {
    }

    public Board(long boardNumber, Key<BiddingSet> biddingSet) {
        this.boardNumber = boardNumber;
        this.biddingSet = biddingSet;
    }

    public long getBoardNumber() {
        return boardNumber;
    }

    public Key<BiddingSet> getBiddingSet() {
        return biddingSet;
    }

    public List<String> getComments() {
        return comments;
    }

    public void setComments(List<String> comments) {
        this.comments = comments;
    }

    public BiddingSequence getBiddingSequence() {
        return biddingSequence;
    }

    public void setBiddingSequence(BiddingSequence biddingSequence) {
        this.biddingSequence = biddingSequence;
    }

    public Distribution getDistribution() {
        return distribution;
    }

    public void setDistribution(Distribution distribution) {
        this.distribution = distribution;
    }

    @PostLoad
    void stringToDistribution() {
        distribution = DistributionFactory.stringToDistribution(distributionStr);
    }

    @PostLoad
    void stringToBiddingSequence() {
        biddingSequence = BiddingSequenceFactory.stringToBiddingSequence(biddingSequenceStr);
    }

    @PrePersist
    void distributionToString() {
        distributionStr = DistributionFactory.distributionToString(distribution);
    }

    @PrePersist
    void biddingSequenceToString() {
        biddingSequenceStr = BiddingSequenceFactory.biddingSequenceToString(biddingSequence);
    }
}
